package ochart.serialization.json.oChart.charts
{
	import ochart.chartClasses.IChartObject;
	import ochart.core.ChartObject;
	import ochart.core.ChartObjectFactory;
	import ochart.core.XYChart;
	import ochart.elements.areas.Area;
	import ochart.elements.axes.RightYAxis;
	import ochart.elements.axes.XAxis;
	import ochart.elements.axes.YAxis;
	import ochart.elements.bars.Bar;
	import ochart.elements.bars.BarBase;
	import ochart.elements.lines.LineBase;
	import ochart.serialization.SerializeFactory;
	import ochart.serialization.json.JSON;
	import ochart.serialization.json.oChart.JSONSerializer;
	import ochart.utils.ArrayUtil;
	import ochart.utils.ChartUtil;
	
	public class XYChartSerializer extends ChartSerializer
	{
		private static var _instance:XYChartSerializer = null;
		
		public static function get instance():XYChartSerializer
		{
			if(!_instance)
			{
				_instance = new XYChartSerializer();
			}
			return _instance;
		}
		
		public function XYChartSerializer()
		{
		}
		
		override public function fromJSON(json:Object):Boolean
		{
			if(!super.fromJSON(json))
			{
				return false;
			}
			
			var chart:XYChart = this.target as XYChart;
			chart.hasBar = ChartUtil.hasBar(chart.data);
			if(json[XAxis.ALIAS])
			{
				var xAxis:XAxis = ChartObjectFactory.instance.create(XAxis.ALIAS) as XAxis;
				xAxis.chart = chart;
				xAxis.data = json[XAxis.ALIAS];
				chart.xAxis = xAxis;
			}
			if(json[YAxis.ALIAS])
			{
				var yAxis:YAxis = ChartObjectFactory.instance.create(YAxis.ALIAS) as YAxis;
				yAxis.chart = chart;
				yAxis.data = json[YAxis.ALIAS];
				chart.yAxis = yAxis;
			}
			if(json[RightYAxis.ALIAS])
			{
				var rightYAxis:RightYAxis = ChartObjectFactory.instance.create(RightYAxis.ALIAS) as RightYAxis;
				rightYAxis.chart = chart;
				rightYAxis.data = json[RightYAxis.ALIAS];
				chart.rightYAxis = rightYAxis;
			}
			
			if(json["elements"])
			{
				var elements:Object = json["elements"];
				
				/* 用以确定有多少个Bar,平均分配她们的高宽 */
				var barAliasArray:Array = [Bar.ALIAS];
				var barCount:int = 0;
				for each(var barData:Object in elements)
				{
					if(barData && ArrayUtil.containsValue(barAliasArray, barData["type"]))
					{
						barCount++;
					}
				}
				
				var barIndex:int = 0;
				var elementsArray:Array = [];
				for each(var elementData:Object in elements)
				{
					if(!elementData)
					{
						continue;
					}
					var element:ChartObject = ChartObjectFactory.instance.create(elementData["type"]) as ChartObject;
					element.chart = chart;
					if(ArrayUtil.containsValue(barAliasArray, element.alias))
					{
						(element as BarBase).barCount = barCount;
						(element as BarBase).barIndex = barIndex;
					}
					element.data = elementData;
					if(chart.xAxis)
					{
						element.width = chart.xAxis.width - (chart.xAxis.offset * 2);
					}
					if(chart.yAxis)
					{
						element.height = chart.yAxis.height - (chart.yAxis.offset * 2);
					}
					elementsArray.push(element);
					barIndex++;
				}
				chart.elementsArray = elementsArray;
			}
			
			return true;
		}
		
		override public function toJSON():JSON
		{
			var json:JSON = super.toJSON();
			return json;
		}
	}
}